﻿using PDO.DataPersistence.ServiceProvider;
using System.Reflection;
using System.Windows;
using System.IO;
using PDO.DataPersistence.Core;


namespace PDO.FX.App.DataExplorer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ExplorerWindow : Window
    {
        #region Variables

        private DataExplorerPM _pm;

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        public ExplorerWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(View_Loaded);
        }

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public DataExplorerPM PM
        {
            get { return _pm; }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void View_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeDataContext();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loadButton_Click(object sender, RoutedEventArgs e)
        {
            PersistableDataObjectClassInfo pdoinfo = _classCombo.SelectedItem as PersistableDataObjectClassInfo;
            PropertyInfo pi = _propertyInfoCombo.SelectedItem as PropertyInfo;

            dataExplorerControl.Initialize(pdoinfo, pi, _propertyValueTextBox.Text);
        }

        #endregion

        #region Overriden Methods

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            Properties.Settings.Default.Save();
        }


        /// <summary>
        /// 
        /// </summary>
        public void InitializeDataContext()
        {
            // Create PM and attach to DataContext in load event handler instead of in constructor
            // to improve plugin load time and guarantee business logic isn't executed until after 
            // successful login.
            if (null == PM)
            {
                _pm = new DataExplorerPM();
                DataContext = PM;
            }

            // Check for first time start-up and supply some defaults.
            if (string.IsNullOrEmpty(PM.DBConnectionString) && string.IsNullOrEmpty(PM.DataModelAssemblyFilePath))
            {
                PM.DBConnectionString = "Data Source=localhost;Initial Catalog=Northwind_PDO_Sample;Persist Security Info=True;Integrated Security=True;Pooling=False";
                PM.DataModelAssemblyFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"NorthwindDataModel.dll");

                DisplaySettingsDialog();
            }
        }

        #endregion

        private void _evaluatorButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataExplorerControl.SelectedTreeItem != null &&
                dataExplorerControl.SelectedTreeItem.PersistableData != null)
            {
                ExpressionEvaluatorPopup dlg = new ExpressionEvaluatorPopup(
                    dataExplorerControl.SelectedTreeItem.PersistableData as PersistableDataObject);
                dlg.Show();
            }
        }

        private void DisplaySettingsDialog()
        {
            SettingsPopup dlg = new SettingsPopup();
            dlg.DataContext = PM;
            if (dlg.ShowDialog() == true)
            {

                System.Data.SqlClient.SqlConnectionStringBuilder scb = new System.Data.SqlClient.SqlConnectionStringBuilder(PM.DBConnectionString);
                IDataObjectSourceProvider dataProvider = new DataObjectSourceDirect(scb);
                DataSession.SetDataSourceProvider(dataProvider);

                PM.LoadDataModelAssemblyFilePath(PM.DataModelAssemblyFilePath);
            }
        }

        private void settingsMenuItem_Click(object sender, RoutedEventArgs e)
        {
            DisplaySettingsDialog();
        }

    }
}